﻿using System;
using System.Collections.Generic;
using RootMotion.FinalIK;
using UnityEngine;

// Token: 0x02000154 RID: 340
public class IK_Control
{
	// Token: 0x060008B1 RID: 2225 RVA: 0x00032920 File Offset: 0x00030B20
	public IK_Control(FullBodyBipedIK fbik, Transform tinRoot, Transform tinTip, Transform mouthTrans)
	{
		this.fbik = fbik;
		this.tinRoot = tinRoot;
		this.tinTip = tinTip;
		this.mouthTrans = mouthTrans;
		fbik.solver.leftShoulderEffector.target = null;
		fbik.solver.rightShoulderEffector.target = null;
	}

	// Token: 0x17000118 RID: 280
	// (get) Token: 0x060008B2 RID: 2226 RVA: 0x00032980 File Offset: 0x00030B80
	public FullBodyBipedIK FBIK
	{
		get
		{
			return this.fbik;
		}
	}

	// Token: 0x17000119 RID: 281
	// (get) Token: 0x060008B3 RID: 2227 RVA: 0x00032988 File Offset: 0x00030B88
	// (set) Token: 0x060008B4 RID: 2228 RVA: 0x00032990 File Offset: 0x00030B90
	public bool TinEnable { get; set; }

	// Token: 0x1700011A RID: 282
	// (get) Token: 0x060008B5 RID: 2229 RVA: 0x0003299C File Offset: 0x00030B9C
	// (set) Token: 0x060008B6 RID: 2230 RVA: 0x000329A4 File Offset: 0x00030BA4
	public bool MouthEnable { get; set; }

	// Token: 0x060008B7 RID: 2231 RVA: 0x000329B0 File Offset: 0x00030BB0
	public void Update()
	{
		this.UpdateBlends();
	}

	// Token: 0x060008B8 RID: 2232 RVA: 0x000329B8 File Offset: 0x00030BB8
	private void UpdateBlends()
	{
		for (int i = 0; i < this.blends.Count; i++)
		{
			if (this.blends[i].Update(Time.deltaTime))
			{
				this.blends.RemoveAt(i);
				i--;
			}
		}
	}

	// Token: 0x060008B9 RID: 2233 RVA: 0x00032A0C File Offset: 0x00030C0C
	public void ClearIK()
	{
		this.fbik.solver.leftHandEffector.positionWeight = 0f;
		this.fbik.solver.leftHandEffector.rotationWeight = 0f;
		this.fbik.solver.leftArmChain.bendConstraint.weight = 0f;
		this.fbik.solver.rightHandEffector.positionWeight = 0f;
		this.fbik.solver.rightHandEffector.rotationWeight = 0f;
		this.fbik.solver.rightArmChain.bendConstraint.weight = 0f;
		this.fbik.solver.leftFootEffector.positionWeight = 0f;
		this.fbik.solver.leftFootEffector.rotationWeight = 0f;
		this.fbik.solver.leftLegChain.bendConstraint.weight = 0f;
		this.fbik.solver.rightFootEffector.positionWeight = 0f;
		this.fbik.solver.rightFootEffector.rotationWeight = 0f;
		this.fbik.solver.rightLegChain.bendConstraint.weight = 0f;
		this.fbik.solver.leftShoulderEffector.positionWeight = 0f;
		this.fbik.solver.rightShoulderEffector.positionWeight = 0f;
		this.fbik.solver.bodyEffector.positionWeight = 0f;
		this.tinTarget = null;
		this.mouthTarget = null;
		this.blends.Clear();
	}

	// Token: 0x060008BA RID: 2234 RVA: 0x00032BCC File Offset: 0x00030DCC
	public void ClearIK(IK_Data.PART part)
	{
		if (part == IK_Data.PART.HAND_L)
		{
			this.fbik.solver.leftHandEffector.positionWeight = 0f;
			this.fbik.solver.leftHandEffector.rotationWeight = 0f;
			this.fbik.solver.leftArmChain.bendConstraint.weight = 0f;
		}
		if (part == IK_Data.PART.HAND_R)
		{
			this.fbik.solver.rightHandEffector.positionWeight = 0f;
			this.fbik.solver.rightHandEffector.rotationWeight = 0f;
			this.fbik.solver.rightArmChain.bendConstraint.weight = 0f;
		}
		if (part == IK_Data.PART.FOOT_L)
		{
			this.fbik.solver.leftFootEffector.positionWeight = 0f;
			this.fbik.solver.leftFootEffector.rotationWeight = 0f;
			this.fbik.solver.leftLegChain.bendConstraint.weight = 0f;
		}
		if (part == IK_Data.PART.FOOT_R)
		{
			this.fbik.solver.rightFootEffector.positionWeight = 0f;
			this.fbik.solver.rightFootEffector.rotationWeight = 0f;
			this.fbik.solver.rightLegChain.bendConstraint.weight = 0f;
		}
		if (part == IK_Data.PART.TIN)
		{
			this.tinTarget = null;
		}
	}

	// Token: 0x060008BB RID: 2235 RVA: 0x00032D50 File Offset: 0x00030F50
	public void SetIK(IK_Data.PART part, Transform target, float speed = 0f)
	{
		if (part == IK_Data.PART.TIN)
		{
			this.tinTarget = target;
			return;
		}
		if (part == IK_Data.PART.MOUTH)
		{
			this.mouthTarget = target;
			this.mouthSpeed = speed;
			if (speed <= 0f)
			{
				this.mouthRate = 1f;
			}
			return;
		}
		IKEffector effector = this.GetEffector(part);
		effector.target = target;
		FBIKChain chain = this.GetChain(part);
		if (speed <= 0f)
		{
			effector.positionWeight = 1f;
			effector.rotationWeight = 1f;
			chain.bendConstraint.weight = 1f;
		}
		else
		{
			this.AddBlend(effector, chain, speed);
		}
	}

	// Token: 0x060008BC RID: 2236 RVA: 0x00032DF0 File Offset: 0x00030FF0
	private void AddBlend(IKEffector effector, FBIKChain chain, float speed)
	{
		int num = -1;
		for (int i = 0; i < this.blends.Count; i++)
		{
			if (this.blends[i].Check(effector, chain))
			{
				num = i;
				break;
			}
		}
		if (num == -1)
		{
			this.blends.Add(new IKBlendData(effector, chain, speed));
		}
		else
		{
			this.blends[num] = new IKBlendData(effector, chain, speed);
		}
	}

	// Token: 0x060008BD RID: 2237 RVA: 0x00032E70 File Offset: 0x00031070
	private IKEffector GetEffector(IK_Data.PART part)
	{
		IKEffector[] array = new IKEffector[5];
		array[0] = this.fbik.solver.leftHandEffector;
		array[1] = this.fbik.solver.rightHandEffector;
		array[2] = this.fbik.solver.leftFootEffector;
		array[3] = this.fbik.solver.rightFootEffector;
		IKEffector[] array2 = array;
		return array2[(int)part];
	}

	// Token: 0x060008BE RID: 2238 RVA: 0x00032ED4 File Offset: 0x000310D4
	private FBIKChain GetChain(IK_Data.PART part)
	{
		FBIKChain[] array = new FBIKChain[5];
		array[0] = this.fbik.solver.leftArmChain;
		array[1] = this.fbik.solver.rightArmChain;
		array[2] = this.fbik.solver.leftLegChain;
		array[3] = this.fbik.solver.rightLegChain;
		FBIKChain[] array2 = array;
		return array2[(int)part];
	}

	// Token: 0x060008BF RID: 2239 RVA: 0x00032F38 File Offset: 0x00031138
	public void CalcTin()
	{
		if (this.tinRoot == null || this.tinTarget == null || !this.TinEnable)
		{
			return;
		}
		Vector3 v = this.tinRoot.parent.InverseTransformPoint(this.tinTarget.position);
		float y;
		float x;
		VectorUtility.Vector3_ToYawPitch(v, out y, out x);
		this.tinRoot.localRotation = Quaternion.Euler(x, y, 0f);
		this.tinTip.position = this.tinTarget.position;
	}

	// Token: 0x060008C0 RID: 2240 RVA: 0x00032FC8 File Offset: 0x000311C8
	public void SetMouthTrans(Transform mouth)
	{
		this.mouthTrans = mouth;
	}

	// Token: 0x060008C1 RID: 2241 RVA: 0x00032FD4 File Offset: 0x000311D4
	public void CalcMouth()
	{
		if (this.mouthTrans == null || this.mouthTarget == null)
		{
			return;
		}
		if (this.mouthRate < 1f)
		{
			this.mouthRate += Time.deltaTime * this.mouthSpeed;
			this.mouthRate = Mathf.Min(this.mouthRate, 1f);
		}
		float num = this.mouthRate;
		float num2 = 1f;
		float num3 = 1f;
		this.fbik.solver.leftShoulderEffector.target = null;
		this.fbik.solver.rightShoulderEffector.target = null;
		this.fbik.solver.leftShoulderEffector.positionWeight = num;
		this.fbik.solver.rightShoulderEffector.positionWeight = num;
		this.fbik.solver.bodyEffector.positionWeight = num;
		this.InverseTransformEffector(FullBodyBipedEffector.LeftShoulder, this.mouthTrans, this.mouthTarget.position, num);
		this.InverseTransformEffector(FullBodyBipedEffector.RightShoulder, this.mouthTrans, this.mouthTarget.position, num);
		this.InverseTransformEffector(FullBodyBipedEffector.Body, this.mouthTrans, this.mouthTarget.position, num);
		IKEffector bodyEffector = this.fbik.solver.bodyEffector;
		bodyEffector.position = Vector3.Lerp(new Vector3(bodyEffector.position.x, bodyEffector.bone.position.y, bodyEffector.position.z), bodyEffector.position, num2 * num);
		bodyEffector.position = Vector3.Lerp(new Vector3(bodyEffector.bone.position.x, bodyEffector.position.y, bodyEffector.bone.position.z), bodyEffector.position, num3 * num);
	}

	// Token: 0x060008C2 RID: 2242 RVA: 0x000331B0 File Offset: 0x000313B0
	private void InverseTransformEffector(FullBodyBipedEffector effector, Transform target, Vector3 targetPosition, float weight)
	{
		Vector3 b = this.fbik.solver.GetEffector(effector).bone.position - target.position;
		this.fbik.solver.GetEffector(effector).position = Vector3.Lerp(this.fbik.solver.GetEffector(effector).bone.position, targetPosition + b, weight);
	}

	// Token: 0x04000849 RID: 2121
	private FullBodyBipedIK fbik;

	// Token: 0x0400084A RID: 2122
	private Transform tinRoot;

	// Token: 0x0400084B RID: 2123
	private Transform tinTip;

	// Token: 0x0400084C RID: 2124
	private Transform tinTarget;

	// Token: 0x0400084D RID: 2125
	private Transform mouthTrans;

	// Token: 0x0400084E RID: 2126
	private Transform mouthTarget;

	// Token: 0x0400084F RID: 2127
	private float mouthRate;

	// Token: 0x04000850 RID: 2128
	private float mouthSpeed;

	// Token: 0x04000851 RID: 2129
	private bool mouthGoal;

	// Token: 0x04000852 RID: 2130
	private List<IKBlendData> blends = new List<IKBlendData>();
}
